const p1 = data =>
  new Promise((resole, reject) =>
    setTimeout(() => {
      console.log(data + ' - p1')
      resole('p1')
    }, 1000)
  )

const p2 = data =>
  new Promise((resole, reject) =>
    setTimeout(() => {
      console.log(data + ' - p2')
      resole('p2')
    }, 1000)
  )

const p3 = data =>
  new Promise(resole =>
    setTimeout(() => {
      console.log(data + ' - p3')
      resole('p3')
    }, 1000)
  )

const list = [p1, p2, p3]

// [p1, p2, p3].reduce(
//   (prev, current) => prev.then(current).catch((e) => e),
//   Promise.resolve("init")
// );

/**
 * 依次调用 promise
 * @param promiseList
 * @returns {Promise<string>}
 */
async function promise_queue(promiseList) {
  let res = 'init'
  const result = []

  for (let index = 0; index < promiseList.length; index++) {
    const promise = promiseList[index]
    try {
      res = await promise(res)
      result.push(res)
    } catch (e) {
      console.log('错误: 跳出', e)
      throw new Error('抛出错误' + e)
    }
  }

  return '结果' + result
}

promise_queue(list)
  .then(r => console.log(r))
  .catch(e => console.log(e))
